package com.sthz.facepay.controller;

import com.sthz.facepay.enums.ApiResultEnum;
import com.sthz.facepay.model.User;
import com.sthz.facepay.model.result.ApiResult;
import com.sthz.facepay.service.UserService;
import com.sthz.facepay.util.ApiResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用户
 * @author luli
 * @version jdk 1.8
 * @date 2019/7/12 11:19
 */
@Controller
@RequestMapping(value = "/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 终端商户登陆接口
     */
    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ApiResult login(String name, String password) {
        //身份验证是否成功
        boolean isSuccess = userService.checkUser(name, password);
        if (isSuccess) {
            User user = userService.getUserByLoginName(name);
            return ApiResultUtil.getApiResult(user,ApiResultEnum.SUCCESS.getErrCode(),ApiResultEnum.SUCCESS.getErrMsg());
            /*User user = userService.getUserByLoginName(name);
            if (user != null) {
                //返回token
                String token = JwtUtil.sign(user.getUsername(), user.getId());
                if (token != null) {
                    return ApiResultUtil.getApiResult(token);
                }
            }*/
        }
        //返回登陆失败消息
        return ApiResultUtil.getApiResult(ApiResultEnum.LOGIN_FAIL);
    }

    /**
     * 修改用户信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/modifyUserInfo", method = RequestMethod.POST)
    public ApiResult update(User user) {
        User u = new User();
        int code = userService.modifyUserInfo(user);
        if(code == 10000) {
            u = userService.selectByPrimaryKey(user.getId());
            return ApiResultUtil.getApiResult(u,ApiResultEnum.SUCCESS.getErrCode(),ApiResultEnum.SUCCESS.getErrMsg());
        }
        return ApiResultUtil.getApiResult(u,ApiResultEnum.FAIL.getErrCode(),ApiResultEnum.FAIL.getErrMsg());
    }

    /**
     * 修改用户密码
     * @param id
     * @param password
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/modifyPassword", method = RequestMethod.POST)
    public ApiResult updatePassword(int id, String password) {
        int code = userService.modifyPassword(id,password);
        if(code == 10000) {
            return ApiResultUtil.getApiResult(ApiResultEnum.SUCCESS.getErrCode(),ApiResultEnum.SUCCESS.getErrMsg());
        }
        return ApiResultUtil.getApiResult(ApiResultEnum.FAIL.getErrCode(),ApiResultEnum.FAIL.getErrMsg());
    }
}
